import store from '@/store.js';

export const getMatchCommonEvents = events => {
  const evtKeys = [];
  events.forEach(n => {
    if (n.type === 'in') {
      evtKeys.push(n.key);
    }
  });
  if (!evtKeys.length) {
    return [];
  }

  const { commonParams } = store.state.VisualComponentsModule;
  if (!commonParams || !Object.keys(commonParams).length) {
    return [];
  }

  const matchEvents = [];
  Object.entries(commonParams).map(([wid]) => {
    const { evtKey, params } = commonParams[wid];
    if (evtKeys.includes(evtKey)) {
      matchEvents.push({
        evtKey,
        params,
        sentByWid: wid,
      });
    }
  });

  return matchEvents;
};

// 从事件中提取公共参数数组
export const getMatchCommonParams = matchEvents => {
  const paramsMap = {};
  matchEvents.forEach(evt => {
    for (let i = 0; i < evt.params.length; i++) {
      const n = evt.params[i];
      paramsMap[n.key] = {
        ...n,
        sendTime: 1,
        wid: evt.sentByWid,
      };
    }
  });
  const matchParams = Object.values(paramsMap);
  return matchParams;
};

/*
 * 将公共参数添加到已有参数数组中
 * xMiner 与 xtag 的params入参不同
 */
export const addCommonParamsBySourceType = (dataSourceType, params, matchParams) => {
  let _params = params;
  if (['xMiner', 'xMinerPublicDatabase', 'fabric', 'Dac'].includes(dataSourceType)) {
    _params = _params || [];
    for (let k = 0; k < matchParams.length; k++) {
      _params = _params.filter(n => !n.isCommon || (n.isCommon && n.wid !== matchParams[k].wid));
      for (let i = 0; i < matchParams.length; i++) {
        _params.push({
          sendTime: 1,
          code: matchParams[i].key,
          value: matchParams[i].value,
          wid: matchParams[i].wid,
          isCommon: 1,
        });
      }
    }
    return _params;
  } else if (dataSourceType === 'xTag') {
    if (!matchParams.length) {
      return false;
    }

    let { conditions = [] } = params;
    const { sqlParams = [] } = params;
    conditions = conditions.filter(n => !n.isCommon);
    const item = { terms: [], isCommon: 1 };
    const { terms } = item;

    for (let i = 0; i < matchParams.length; i++) {
      const { key, value } = matchParams[i];

      // conditions 部分
      const hasParam = terms.some(n => {
        const keys = Object.keys(n);
        return keys.includes(key);
      });
      if (!hasParam) {
        terms.push({ [key]: [value] });
      }
      conditions.push(item);
      params.conditions = conditions;

      // sqlParams 部分
      const sqlItem = sqlParams.find(n => n.key === key);
      if (!sqlItem) {
        sqlParams.push({ key, value });
      } else {
        sqlItem.value = value;
      }
    }
    return null;
  } else {
    return null;
  }
};

// 获取需要重新触发事件的事件key
export const getCommonParamsUpdatedEvtKeys = () => {
  const evtKeys = [];
  const { commonParams, commonParamsDiff } = store.state.VisualComponentsModule;

  for (let i = 0; i < commonParamsDiff.length; i++) {
    const n = commonParamsDiff[i];
    Object.entries(n).map(([wid]) => {
      const evItem = n[wid];
      const evtKey = evItem.evtKey || commonParams[wid].evtKey; // 对象相减后不一定有evtKey，这时取状态装的key
      evtKeys.push(evtKey);
    });
  }

  return evtKeys;
};

export const formatEvents = matchEvents => {
  const fEvents = matchEvents.map(n => {
    const parameters = {};
    n.params.forEach(p => {
      parameters[p.key] = p.value;
    });

    return {
      eventKey: n.evtKey,
      parameters,
      widgetInstanceId: n.sentByWid,
      sendTime: 0,
    };
  });
  return fEvents;
};
